import React, { forwardRef, useImperativeHandle } from 'react';
import { useHistory } from 'react-router-dom';
import { Table, Space, Button, Modal, message } from 'antd';
import { Authorized } from '@/components';
import permission from '@/config/permission';
import enums from '@/enums';
import { useAcceptanceList, useDeleteAcceptance } from './hooks';

/**
 * 验收列表
 */
function AcceptanceList(props, ref) {
    const { form } = props;

    const history = useHistory();

    const [{ confirm }, contextHolder] = Modal.useModal();

    const acceptanceList = useAcceptanceList({ form });
    const deleteAcceptance = useDeleteAcceptance({
        onSuccess() {
            acceptanceList.refresh();
            message.success('删除成功');
        },
        onError() {
            message.error('删除失败');
        },
    });

    useImperativeHandle(ref, () => acceptanceList);

    return (
        <>
            <Table
                rowKey="acceptanceMaterialId"
                scroll={{ x: 'max-content' }}
                columns={[
                    {
                        fixed: 'left',
                        width: 180,
                        title: '验收资料名称/ID',
                        dataIndex: 'acceptanceMaterialName',
                        render(text, { acceptanceMaterialId }) {
                            return (
                                <>
                                    <div>{text}</div>
                                    <div>{acceptanceMaterialId}</div>
                                </>
                            );
                        },
                    },
                    {
                        width: 160,
                        title: '云区域归属省分',
                        dataIndex: 'cloudRegionProvinceName',
                    },
                    {
                        width: 160,
                        title: '工单编号',
                        dataIndex: 'workOrderNumber',
                        render: (text) => (
                            <Button
                                style={{ padding: 0 }}
                                type="link"
                                onClick={() => {
                                    window.open(
                                        `${window.location.origin}/ops/portal/work-order/query/existed-list/detail?id=${text}&status=query`,
                                        '_blank'
                                    );
                                }}
                            >
                                {text}
                            </Button>
                        ),
                    },
                    {
                        width: 160,
                        title: '状态',
                        dataIndex: 'acceptanceStatus',
                        render: (value) => enums.ossAcceptance.acceptanceStatus.get(value) ?? '-',
                    },
                    {
                        width: 160,
                        title: '创建人',
                        dataIndex: 'creatorName',
                        render: (text) => text ?? '-',
                    },
                    {
                        width: 160,
                        title: '创建时间',
                        dataIndex: 'createTime',
                    },
                    {
                        width: 160,
                        title: '最后更新人',
                        dataIndex: 'updatePersonName',
                        render: (text) => text ?? '-',
                    },
                    {
                        width: 160,
                        title: '最后更新时间',
                        dataIndex: 'updateTime',
                    },
                    {
                        fixed: 'right',
                        width: 180,
                        title: '操作',
                        render: ({ acceptanceMaterialId, acceptanceStatus, acceptanceMaterialName }) => (
                            <Space>
                                <Authorized authority={permission.resourceAcceptance_view} noMatch={null}>
                                    <Button
                                        style={{ padding: 0 }}
                                        type="link"
                                        onClick={() => {
                                            history.push(`/resource/acceptance/detail?id=${acceptanceMaterialId}`);
                                        }}
                                    >
                                        查看
                                    </Button>
                                </Authorized>
                                <Authorized authority={permission.resourceAcceptance_edit} noMatch={null}>
                                    <Button
                                        style={{ padding: 0 }}
                                        type="link"
                                        disabled={
                                            ![
                                                'IN_PROGRESS',
                                                'FAILED',
                                                'IN_PROGRESS_WAIT_SUBMIT',
                                                'FAILED_WAIT_SUBMIT',
                                            ].includes(acceptanceStatus)
                                        }
                                        onClick={() => {
                                            history.push(`/resource/acceptance/edit?id=${acceptanceMaterialId}`);
                                        }}
                                    >
                                        编辑
                                    </Button>
                                </Authorized>
                                <Authorized authority={permission.resourceAcceptance_delete} noMatch={null}>
                                    <Button
                                        style={{ padding: 0 }}
                                        type="link"
                                        disabled={!['FAILED', 'FAILED_WAIT_SUBMIT'].includes(acceptanceStatus)}
                                        onClick={() => {
                                            confirm({
                                                title: '删除验收资料',
                                                content: (
                                                    <>
                                                        <div>确认删除验收资料“{acceptanceMaterialName}”？</div>
                                                        <div>删除后无法恢复当前资料！</div>
                                                    </>
                                                ),
                                                onOk() {
                                                    return deleteAcceptance.runAsync(acceptanceMaterialId);
                                                },
                                            });
                                        }}
                                    >
                                        删除
                                    </Button>
                                </Authorized>
                            </Space>
                        ),
                    },
                ]}
                {...acceptanceList.tableProps}
                pagination={{
                    showSizeChanger: true,
                    showTotal: (total) => `共 ${total} 条`,
                    ...acceptanceList.tableProps.pagination,
                }}
            />
            {contextHolder}
        </>
    );
}

export default forwardRef(AcceptanceList);
